<?php
/**
 * Created by PhpStorm.
 * User: zhd
 * Date: 2017/12/10
 * Time: 20:02
 */

namespace controller;

use controller\base\Base;
use extend\wechat\JSSDK;
use extend\wechat\Media;
use model\Images;
use model\Releases;

class Bm extends Base
{
    /**
     * @param int $id id
     */
    public function index($id)
    {
        $this->header($id);
        $releases = new Releases();
        $data     = $releases->row([
            'openid'    => session('openid'),
            'action_id' => $id
        ]);
        if (!empty($data)) {
            $rid = $data['id'];
            redirect(url('Index/view', ['id' => $id, 'rid' => $rid]));
        } else {
            $this->fetch();
        }
    }

    /**
     * @param int $id id
     * @param string $name name
     * @param int $tel tel
     * @param string $serverid serverid
     * @param string $content content
     * @param string $address address
     * @return mixed
     */
    public function post($id, $name, $tel, $serverid, $content, $address)
    {
        $this->header();

        if (!$this->is_releases) {
            return ['state' => false, 'msg' => '还未到报名时间'];
        }
        if ($this->is_releases_end) {
            return ['state' => false, 'msg' => '报名时间已结束'];
        }

        $releases = new Releases();
        $data     = $releases->row([
            'action_id' => $id,
            'openid'    => session('openid')
        ]);
        if (!empty($data)) {
            return json([
                'code' => 'fail',
                'msg'  => '重复发布',
            ]);
        }

        $serverids   = explode(',', $serverid);
        $jssdk       = new JSSDK(url(), $this->wx_id);
        $accessToken = $jssdk->getAccessToken();
        $media       = new Media();
        $images      = [];
        $imagesModel = new Images();
        foreach ($serverids as $serverid) {
            $data       = $media->get($accessToken, $serverid);
            $md5        = md5($data);
            $imagesData = $imagesModel->row(['md5' => $md5]);
            if (empty($imagesData)) {
                $url      = "/upload/img/{$serverid}.jpg";
                $img      = "/yjdata/www/www/upload/img/{$serverid}.jpg";
                $images[] = $url;
                file_put_contents($img, $data);
                $imagesModel->insert([
                    'url'     => $url,
                    'created' => now(),
                    'md5'     => $md5
                ]);
            } else {
                $images[] = $imagesData['url'];
            }

        }
        $images = implode(',', $images);

        $number = $releases->max('number', [
            'action_id' => $id,
            'is_pass'   => 'true'
        ]);

        $releases->insert([
            'action_id' => $id,
            'openid'    => session('openid'),
            'images'    => $images,
            'shop_name' => $name,
            'mobile'    => $tel,
            'address'   => $address,
            'desc'      => $content,
            'number'    => $number + 1,
            'created'   => now(),
            'sort'      => $number + 1
        ]);

        return json([
            'code' => 'success',
            'url'  => url('Bm/index', ['id' => $id]),
            'msg'  => '发布成功'
        ]);
    }
}